package model.location;

import model.Dimension;
import model.Snippet;
import android.util.Log;

public class SearchArea extends WorldArea
{
	private static final String TAG = SearchArea.class.getSimpleName();
	
	private static final float DEPTH_RANGE = 100f;
	
	private int _depth;
	
	public SearchArea(WorldCoordinate upperLeft, Dimension dimension, int depth)
	{
		super((WorldCoordinate) upperLeft, dimension);
		
		_depth = depth;
	}
	
	public boolean contains(Snippet snippet)
	{
		if(isWithinArea(snippet.getWorldArea()))
		{
			Log.d(TAG, String.format("Snippet: %s - isInRect", snippet.getId()));
			return isWithinDepth((int) ((WorldCoordinate) snippet.getWorldArea().getCenterCoord()).getZ());
		}
		
		return false;
	}
	
	private boolean isWithinArea(Area area)
	{
		float[] points = area.getPoints();
		
		if(points[0] >= _points[0] && points[2] <= _points[2])
			if(points[1] <= _points[1] && points[7] >= _points[7])
				return true;
		return false; 			
	}
	
	private boolean isWithinDepth(int depth)
	{
		Log.d(TAG, String.format("SearchDepth: %d\nSnippetDepth: %d", _depth, depth));
		return ((depth >= (_depth - DEPTH_RANGE)) && (depth <= (_depth + DEPTH_RANGE)));
	}

	public WorldCoordinate getUpperLeft() 
	{
		return (WorldCoordinate) _coordinate;
	}
	
	public WorldCoordinate getLowerRight()
	{
		return new WorldCoordinate(_coordinate.getX() + _dimension.getWidth(), _coordinate.getY() - _dimension.getHeight(), _depth);
	}
}
